package com.miukoo.aikao.qmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miukoo.aikao.core.dtos.PageReqDto;
import com.miukoo.aikao.core.dtos.PageResDto;
import com.miukoo.aikao.core.dtos.QuestionAnswerDto;
import com.miukoo.aikao.core.utils.BeanMapperUtils;
import com.miukoo.aikao.qmanage.api.v1.QuestionAnswerApi;
import com.miukoo.aikao.qmanage.mapper.QuestionAnswerMapper;
import com.miukoo.aikao.core.pojos.QuestionAnswer;
import com.miukoo.aikao.qmanage.service.QuestionAnswerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
* 语言设置 服务实现类
* </p>
*
* @author 聪明笨狗
* @since 2020-05-25 13:23
*/
@Service
public class QuestionAnswerServiceImpl extends ServiceImpl<QuestionAnswerMapper, QuestionAnswer> implements QuestionAnswerService, QuestionAnswerApi {

    @Override
    public PageResDto<List<QuestionAnswer>> paging(PageReqDto<QuestionAnswerDto> reqDto) {
        //创建分页对象
        IPage<QuestionAnswer> query = new Page<>(reqDto.getCurrent(), reqDto.getSize());

        //查询条件
        QueryWrapper<QuestionAnswer> wrapper = new QueryWrapper<>();

        //获得数据
        IPage<QuestionAnswer> page = this.page(query, wrapper);
        return PageResDto.result(page.getTotal(),page.getRecords());
     }

    @Override
    public List<QuestionAnswer> listAnswerByRandom(Long quId) {
        QueryWrapper<QuestionAnswer> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(QuestionAnswer::getQuId, quId);
        wrapper.last(" ORDER BY RAND() ");

        return this.list(wrapper);
    }

    @Override
    public List<QuestionAnswerDto> listByQuestion(Long quId) {
        QueryWrapper<QuestionAnswer> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(QuestionAnswer::getQuId, quId);

        List<QuestionAnswer> list = this.list(wrapper);
        if(!CollectionUtils.isEmpty(list)){
            return BeanMapperUtils.mapList(list, QuestionAnswerDto.class);
        }

        return null;
    }


    /**
     * 查找已存在的列表
     * @param quId
     * @return
     */
    public List<Long> findExistsList(Long quId) {
        //返回结果
        List<Long> ids = new ArrayList<>();

        QueryWrapper<QuestionAnswer> wrapper = new QueryWrapper();
        wrapper.lambda().eq(QuestionAnswer::getQuId, quId);
        List<QuestionAnswer> list = this.list(wrapper);

        if (!CollectionUtils.isEmpty(list)) {
            for (QuestionAnswer item : list) {
                ids.add(item.getId());
            }
        }
        return ids;
    }

    @Override
    public void saveAll(Long quId, List<QuestionAnswerDto> list) {

        //最终要保存的列表
        List<QuestionAnswer> saveList = new ArrayList<>();

        //已存在的标签列表
        List<Long> ids = this.findExistsList(quId);

        if(!CollectionUtils.isEmpty(list)){
            for(QuestionAnswerDto item: list){

                //标签ID
                Long id = item.getId();
                QuestionAnswer answer = new QuestionAnswer();
                BeanUtils.copyProperties(item,answer);
                answer.setQuId(quId);

                //补全ID避免新增
                if(ids.contains(id)){
                    ids.remove(id);
                }
                saveList.add(answer);
            }

            //保存标签列表
            if(!CollectionUtils.isEmpty(saveList)) {
                this.saveOrUpdateBatch(saveList);
            }

            //删除已移除
            if(!ids.isEmpty()){
                this.removeByIds(ids);
            }
        }else{
            QueryWrapper<QuestionAnswer> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(QuestionAnswer::getQuId, quId);
            this.remove(wrapper);
        }
    }


}
